package com.yx.fx.minio.web.filter;

import com.yx.fx.minio.web.utils.MinIoUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author 杨智杰
 * @since 2021/8/26 14:28
 */
@Component
public class LinkCheckFilter implements Filter {

    @Value("${server.servlet.context-path:}")
    private String startsWith;

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        if (request.getRequestURI().startsWith(startsWith + "/minio")) {
            if (MinIoUtil.MINIO_IS_LINK) {
                filterChain.doFilter(servletRequest, servletResponse);
            } else {
                servletRequest.getRequestDispatcher("/minio/notLink").forward(servletRequest, servletResponse);
            }
        } else {
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }
}
